using System;
using System.Linq;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class addResourcesToTeam : FunctionBase
    {
        private MarsExplorerEntities dbContext;

        public int TeamID { get; set; }
        public int Agua { get; set; }
        public int Hidrogenio { get; set; }
        public int Comida { get; set; }
        public int Oxigenio { get; set; }
        public int KitMedico { get; set; }
        public int Previsao { get; set; }
        public int Escudo { get; set; }
        public int Fato { get; set; }
        public int Creditos { get; set; }

        public addResourcesToTeam(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            try
            {
                Team t = (from c in dbContext.Teams where c.TeamID == TeamID select c).FirstOrDefault();
                t.Credits -= Creditos;
                for (int i = 1; i <= 8; i++)
                {
                    TeamsResource r = (from x in dbContext.TeamsResources where x.TeamID == TeamID && x.ResourceID == i select x).FirstOrDefault();
                   
                    switch (i)
                    {
                        case 1:
                            r.Value += Agua;
                            break;
                        case 2:
                            r.Value += Hidrogenio;
                            break;
                        case 3:
                            r.Value += Comida;
                            break;
                        case 4:
                            r.Value += Oxigenio;
                            break;
                        case 5:
                            r.Value += KitMedico;
                            break;
                        case 6:
                            r.Value += Previsao;
                            break;
                        case 7:
                            r.Value += Escudo;
                            break;
                        case 8:
                            r.Value += Fato;
                            break;
                    }
                    dbContext.SaveChanges();
                }
                
                return "OK";
            }
            catch (Exception e)
            {

                return "ERROR";
            }
            
        }

        public override void InitializeProperties()
        {
            try
            {
                TeamID = Convert.ToInt32(Parameters["teamid"]);
                Agua = Convert.ToInt32(Parameters["agua"]);
                Hidrogenio = Convert.ToInt32(Parameters["hidrogenio"]);
                Comida = Convert.ToInt32(Parameters["comida"]);
                Oxigenio = Convert.ToInt32(Parameters["oxigenio"]);
                KitMedico = Convert.ToInt32(Parameters["kitmedico"]);
                Previsao = Convert.ToInt32(Parameters["previsao"]);
                Escudo = Convert.ToInt32(Parameters["escudo"]);
                Fato = Convert.ToInt32(Parameters["fato"]);
                Creditos = Convert.ToInt32(Parameters["credito"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}